﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace KnowlegeAssistant
{
    static class RoleUtil
    {
        public static Role getRole(string rolename)
        {
            Role role = null;
            DataTable table = getTableRoles();
            DataRow[] rows = table.Select("role_name = '"+rolename+"'");

            int role_id = int.Parse(rows[0]["role_id"].ToString());
            string rname =  rows[0]["role_name"].ToString();

            role = new Role(role_id,rname);

            DataTable permissionsTable = DatabaseConnect.execSP("GetPermissionsByRole", new List<string>() { role_id.ToString() }).Tables[0];

            List<Permission> permissions = new List<Permission>();

            foreach (DataRow row in permissionsTable.Rows)
            {
                permissions.Add(
                    new Permission(
                          int.Parse(row["permission_id"].ToString()), 
                          row["description"].ToString(),
                          row["permission_name"].ToString()
                          )
                      );
            }

            role.Permissions = permissions;
            
            return role;
        }

        public static DataTable getTableRoles()
        {
            DataTable table = DatabaseConnect.execSP("GetRoles").Tables[0];
            return table;
        }

        public static List<Role> getListRoles()
        {
            List<Role> roles = new List<Role>();
            DataTable table = getTableRoles();

            foreach (DataRow row in table.Rows)
            {
                int role_id = int.Parse(row["role_id"].ToString());
                string rname = row["role_name"].ToString();

                Role role = new Role(role_id, rname);

                DataTable permissionsTable = DatabaseConnect.execSP("GetPermissionsByRole", new List<string>() { role_id.ToString() }).Tables[0];

                List<Permission> permissions = new List<Permission>();

                foreach (DataRow prow in permissionsTable.Rows)
                {
                    permissions.Add(
                        new Permission(
                              int.Parse(prow["permission_id"].ToString()),
                              prow["description"].ToString(),
                              prow["permission_name"].ToString()                              
                              )
                          );
                }

                role.Permissions = permissions;

                roles.Add(role);
            }
            return roles;
        }

        public static bool Exists(string rolename)
        {
            DataTableCollection tables = DatabaseConnect.execSP("GetRoleByName",new List<string> (){rolename}).Tables;
            if (tables[0].Rows.Count == 0)
            {
                return false;
            }
            return true;
        }

        public static bool RoleExistsPermission(string rolename, string permissionname)
        {
            DataTable t = new DataTable();
            List<string> parameters = new List<string>() { rolename, permissionname };
            t = DatabaseConnect.execSP("IsRoleContainsPermission", parameters).Tables[0];
            return t.Rows.Count > 0;
        }

        public static void AddRole(string rolename)
        {
            DatabaseConnect.execSPNonQuery("AddRole", new List<string>() { rolename });
        }

        public static void EditRole(int role_id, string newrolename)
        {
            DatabaseConnect.execSPNonQuery("EditRole", new List<string>() { role_id.ToString(), newrolename });
        }

        public static void DeleteRole(int role_id)
        {
            List<string> parameters = new List<string>() { role_id.ToString() };
            DatabaseConnect.execSP("DeleteRole",parameters);            
        }

        public static bool IsRoleIsUsedByUser(int role_id)
        {
            List<string> parameters = new List<string>() { role_id.ToString() };
            DataTable table = DatabaseConnect.execSP("GetUsersByRole", parameters).Tables[0];

            if (table.Rows.Count == 0)
            { return false; }

            return true;
        }

        public static void DeletePermission(int role_id, int permission_id)
        {
            DatabaseConnect.execSP("DeleteRolePermission", new List<string>() { role_id.ToString(), permission_id.ToString() });
        }

        public static void AddPermission(int role_id, int permission_id)
        {
            DatabaseConnect.execSP("AddPermissionToRole", new List<string>() { role_id.ToString(), permission_id.ToString() });
        }

        public static List<Permission> GetPermissions()
        {
            DataTable permissionstable = new DataTable();
            permissionstable = DatabaseConnect.execSP("GetPermissions").Tables[0];
            List<Permission> permissions = new List<Permission>();
            foreach (DataRow row in permissionstable.Rows)
            {
                Permission p = new Permission(
                    (int)row["permission_id"],
                    row["description"].ToString(),
                    row["permission_name"].ToString()
                    );
                permissions.Add(p);
            }
            return permissions;
        }

        public static List<Permission> GetPermissions(int role_id)
        {
            DataTable permissionstable = new DataTable();
            permissionstable = DatabaseConnect.execSP("GetPermissionsByRole", new List<string>() {role_id.ToString()}).Tables[0];
            List<Permission> permissions = new List<Permission>();
            foreach (DataRow row in permissionstable.Rows)
            {
                Permission p = new Permission(
                    (int)row["permission_id"],
                    row["description"].ToString(),
                    row["permission_name"].ToString()
                    );
                permissions.Add(p);
            }
            return permissions;
        }
    }
}
